嗨我是k66,前五篇我們實作Logo。今天來實作顯示即時系統時間,先上畫面。[自我挑戰7]為保證大家都能照著實作出,本篇有附上所有程式碼。讀者看到太長請不要害怕,重點僅有time.c
的2個函式IoWrite8
、IoRead8()
,與CMOS_ADDRESS
、CMOS_DATA
。
放碼上來!程式碼連結
|-edk2space\edk2\ #若非跟著系列文看上來的新手,edk2安裝請查看Day7
|-OinkBLPkg
|-time.c #重點,看IoWrite8、CMOS_ADDRESS、CMOS_DATA即可。
|-time.h
|-OinkBL.inf
|-OinkBLPkg.dsc
time.c
CMOS地址
與CMOS的地址指向的內容
。舉例如要看此刻是星期幾,就用IoWrite8()將CMOS_ADDRESS寫入0x06,再讓CMOS_DATA透過IoRead8()去讀取。#include<Time.h>
#define CMOS_ADDRESS 0x70
#define CMOS_DATA 0x71
EFI_STATUS
EFIAPI
UefiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
EFI_STATUS Status = EFI_SUCCESS;
UINT8 sec=0;
UINT8 min=0;
UINT8 hour=0;
UINT8 weekday=0;
UINT8 day=0;
UINT8 month=0;
UINT8 year=0;
// sec, 0x00
IoWrite8(CMOS_ADDRESS,0x00);
sec=IoRead8(CMOS_DATA);
// min, 0x02
IoWrite8(CMOS_ADDRESS,0x02);
min=IoRead8(CMOS_DATA);
// hour, 0x04
IoWrite8(CMOS_ADDRESS,0x04);
hour=IoRead8(CMOS_DATA);
// weekday, 0x06
IoWrite8(CMOS_ADDRESS,0x06);
weekday=IoRead8(CMOS_DATA);
// day, 0x07
IoWrite8(CMOS_ADDRESS,0x07);
day=IoRead8(CMOS_DATA);
// month, 0x08
IoWrite8(CMOS_ADDRESS,0x08);
month=IoRead8(CMOS_DATA);
// year, 0x09
IoWrite8(CMOS_ADDRESS,0x09);
year=IoRead8(CMOS_DATA);
Print(L"Oink! Current time is: \n");
Print(L"%02x/%02x/%02x %02x %02x:%02x:%02x\n",year,month,day,weekday,hour,min,sec);
return Status;
}
time.h
#include<Uefi.h>
#include<Library/UefiLib.h>
#include<Library/TimerLib.h>
#include<Library/UefiBootServicesTableLib.h>//gST
#include<Library/IoLib.h>
OinkBL.inf
[Defines]
INF_VERSION = 0x00010006
BASE_NAME = OinkBL #output filename
FILE_GUID = 7d371c5c-1a4d-41f9-9509-67632e5ce129
MODULE_TYPE = UEFI_APPLICATION
VERSION_STRING = 0.1
ENTRY_POINT = UefiMain
#UEFI_HII_RESOURCE_SECTION = TRUE
[Sources]
Time.c
Time.h
[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
ShellPkg/ShellPkg.dec
[LibraryClasses]
UefiApplicationEntryPoint#if .c use UefiMain(), then should use this.
# ShellCEntryLib #If .c use ShellEntryMain(), then use it.
UefiLib
BmpSupportLib
IoLib
TimerLib
[Guids]
gEfiFileInfoGuid
OinkBLPkg.dsc
[Defines]
PLATFORM_NAME = OinkBLPkg #output foldername
PLATFORM_GUID = d6097f53-fca0-4540-a7b3-bb4008086628
PLATFORM_VERSION = 0.1
DSC_SPECIFICATION = 0x00010005
SUPPORTED_ARCHITECTURES = X64
BUILD_TARGETS = DEBUG|RELEASE
[LibraryClasses]
UefiLib|MdePkg/Library/UefiLib/UefiLib.inf
UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf
BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
BmpSupportLib|MdeModulePkg/Library/BaseBmpSupportLib/BaseBmpSupportLib.inf
SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
RegisterFilterLib|MdePkg/Library/RegisterFilterLibNull/RegisterFilterLibNull.inf
TimerLib|MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf
IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
[Components]
OinkBLPkg/OinkBL.inf #進入OinkBL.inf
今天透過IoWrite8
、IoRead8()
、CMOS_ADDRESS
、CMOS_DATA
顯示系統時間。至今已完成Logo和及時系統時間顯示。未來幾天會實作bootloader(進到OS)與使用者互動介面,我們明天見!
今天寫的程式,如圖在shell內系統時間與Windows本機時間不同,想想看這是為什麼?如何解決?